
const resData = require('./res.js'); 

var mysql = require('mysql');
var fs = require('fs');
//render页面
function renderApi(req, res){
  fs.readFile('../front/ju-blog/dist/app.html','utf8', (err, data) => {
      if(err) {
        const {code, data, message} = resData.error;
        const sendData = {
          code: code,
          data: data,
          message: message,
        }
        res.send(sendData);
      }else{
        // sendData = success.dat
        const {code, data, message} = resData.success;
        const sendData = {
          code: code,
          data: data,
          message: message,
        }
        res.send(sendData);
      }
  });
}
//删除文章api
function deleteArticle(req, res){
  const connection = mysql.createConnection({
    host: 'localhost', // 填写你的mysql host
    user: 'root', // 填写你的mysql用户名
    password: '12345678', // 填写你的mysql密码
    database: 'blog'
  })
  const deleteALl = 'delete from articles where autherId = 12312';
  connection.query(deleteALl, async (err, results, filelds) => {
    if(err) {
      const {code, data, message} = resData.error;
      const sendData = {
        code: code,
        data: results,
        message: message,
      }
      res.send(sendData);
    }else{
      // sendData = success.dat
      const {code, data, message} = resData.success;
      const sendData = {
        code: code,
        data: data,
        message: message,
      }
      res.send(sendData);
    }
    connection.release();
  })
}
//获取文章列表
function getArticle(req, res) {
  const connection = mysql.createConnection({
    host: 'localhost', // 填写你的mysql host
    user: 'root', // 填写你的mysql用户名
    password: '12345678', // 填写你的mysql密码
    database: 'blog'
  })
  const queryAll = 'select * from articles';
  connection.query(queryAll, async (err, results, filelds) => {
    if(err) {
      const {code, data, message} = resData.error;
      const sendData = {
        code: code,
        data: data,
        message: message,
      }
      res.send(sendData);
    }else{
      // sendData = success.dat
      const {code, data, message} = resData.success;
      const sendData = {
        code: code,
        data: data,
        message: message,
      }
      res.send(sendData);
    }
    connection.release();
  })
}
//保存文章api
function saveArticle(req, res){
  const connection = mysql.createConnection({
    host: 'localhost', // 填写你的mysql host
    user: 'root', // 填写你的mysql用户名
    password: '12345678', // 填写你的mysql密码
    database: 'blog'
  })
  const {title, content,description,author,authorId} = req.query;
  const insert = `INSERT INTO articles (title, content,description,author,authorId) VALUES('${title}','${content}','${description}','${author}',${authorId})`;
  connection.query(insert, async (err, results, filelds) => {
    if(err) {
      const {code, data, message} = resData.error;
      const sendData = {
        code: code,
        data: data,
        message: message,
      }
      res.send(sendData);
    }else{
      // sendData = success.dat
      const {code, data, message} = resData.success;
      const sendData = {
        code: code,
        data: data,
        message: message,
      }
      res.send(sendData);
    }
    connection.release();
  })
}
module.exports = { //api服务
  renderApi,
  getArticle,
  deleteArticle,
  saveArticle
}